#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <pthread.h>

char key_val;

int main (int argc, char ** argv)
{
	int fd = 0;
	pthread_t pt_key;
	unsigned char data = 0;
	int nbytes = 0;
	float val = 0;

	if (argc < 3) {
		printf ("usage: ./app_pcf8591 /dev/pcf8591 <channel>\n");
		printf ("<channel>: 0 1 3\n");
		printf ("  0: brightness\n");//光照
		printf ("  1: tempeature\n");//温度
		printf ("  3: volatge\n");
	}

	fd = open (argv[1], O_RDWR);
	if (fd < 0) {
		perror ("app open error!\n");
		return -1;
	}
		data = (unsigned char)atoi (argv[2]);
		nbytes = read (fd, &data, 1);
		nbytes = write (fd, &data, 1);
		
		val = (float)data * (3.3/255);
		printf ("ADC%d = %.3fV\r\n", atoi(argv[2]), val);
		if(data==1&&val<1)
		{
			system("./led 1");
		}else if(data==1&&val>2)
		{
			system("./led 2");
		}
		if(data==2&&val<1)
		{
			system("./motor 1");
		}else if(data==2&&val>2)
		{
			system("./motor 2");
		}
		usleep (200000);
		
	close (fd);
	
	return 0;
}
